﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using Common;

public class LoginPanel : BasePanel
{
    private InputField usernameIF;
    private InputField passwordIF;
    private LoginRequest loginRequest;

    public override void OnEnter()
    {
        base.OnEnter();

        transform.Find("CloseBtn").GetComponent<Button>().onClick.AddListener(OnCloseBtnClick);
        usernameIF = transform.Find("Username/UsernameInput").GetComponent<InputField>();
        passwordIF = transform.Find("Password/PasswordInput").GetComponent<InputField>();
        transform.Find("LoginBtn").GetComponent<Button>().onClick.AddListener(OnLoginBtnClick);
        transform.Find("RegisterBtn").GetComponent<Button>().onClick.AddListener(OnRegisterBtnClick);
        loginRequest = GetComponent<LoginRequest>();
    }
    private void OnCloseBtnClick()
    {
        uiMng.ClosePanel(UIPanelType.Login);
        uiMng.OpenPanel(UIPanelType.Start);
    }
    private void OnLoginBtnClick()
    {
        if (string.IsNullOrEmpty(usernameIF.text))
        {
            uiMng.ShowMessage("用户名不能为空");
            return;
        }
        if (string.IsNullOrEmpty(passwordIF.text))
        {
            uiMng.ShowMessage("密码不能为空");
            return;
        }
        loginRequest.SendRequest(usernameIF.text, passwordIF.text);
    }
    private void OnRegisterBtnClick()
    {
        uiMng.HidePanel(UIPanelType.Login);
        uiMng.OpenPanel(UIPanelType.Register);
    }
    public void OnLoginResponse(ReturnCode returnCode)
    {
        string msg = "";
        if (returnCode == ReturnCode.Success)
        {
            msg = "登陆成功";
        }
        else
        {
            msg = "用户名或密码错误，无法登录，请重新输入";
        }
        uiMng.ShowAsyncMessage(msg);
    }
    public override void OnHide()
    {
        base.OnHide();
        gameObject.SetActive(false);
    }
}
